package com.sawyerqiu.util;


import com.spire.doc.*;
import com.spire.doc.documents.ImageType;
import org.apache.commons.collections4.CollectionUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;

public class WordUtil {

    public static void translateWordToJPGWithParam(InputStream resourceAsStream, String targetPath, List<String> params) throws IOException {
        //加载Word文档
        Document doc = new Document();
        doc.loadFromStream(resourceAsStream, FileFormat.Docx);

        doc.setReplaceFirst(true);

        if (!CollectionUtils.isEmpty(params)) {
            for (String param : params) {
                doc.replace("${}", param, false, true);
            }
        }

        //保存为PDF格式

        //Convert the whole document into individual buffered images
        BufferedImage[] images = doc.saveToImages(ImageType.Bitmap);

        //Loop through the images
        for (int i = 0; i < images.length; i++) {

            //Get the specific image
            BufferedImage image = images[i];

            //Re-write the image with a different color space
            BufferedImage newImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            newImg.getGraphics().drawImage(image, 0, 0, null);

            //Write to a JPG file
            File file = new File(targetPath + String.format(("-%d.jpg"), i));
            ImageIO.write(newImg, "JPEG", file);
        }
    }

    public static void translateWordToPNGWithParam(InputStream resourceAsStream, String targetPath, List<String> params) throws IOException {
        //加载Word文档
        Document doc = new Document();
        doc.loadFromStream(resourceAsStream, FileFormat.Docx);

        doc.setReplaceFirst(true);

        if (!CollectionUtils.isEmpty(params)) {
            for (String param : params) {
                doc.replace("${}", param, false, true);
            }
        }

        //保存为PDF格式

        //Convert the whole document into individual buffered images

        BufferedImage[] images = doc.saveToImages(0, doc.getPageCount(), ImageType.Bitmap, 150, 150);

//Loop through the images
        for (int i = 0; i < images.length; i++) {

//Get the specific image
            BufferedImage image = images[i];

//Write to a PNG file
            File file = new File(targetPath + String.format(("-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }
    }


    /**
     * 生成模板文件
     *
     * @param prototypePath
     * @param filePath
     * @param count
     * @param originRowInt
     */
    public static void generatePrototype(String prototypePath, String filePath, int count, int originRowInt) throws FileNotFoundException {
        InputStream inputStream;
        File file = new File(prototypePath);
        // 根目录有模板文件有限使用根目录的
        if (file.exists()) {
            inputStream = new FileInputStream(file);
        } else {
            inputStream = WordUtil.class.getClassLoader().getResourceAsStream(prototypePath);
        }

        Document doc = new Document();
        doc.loadFromStream(inputStream, FileFormat.Docx);

        //获取表格
        Section section = doc.getSections().get(0);
        Table table = section.getTables().get(0);

        TableRow originRow = table.getRows().get(originRowInt);

        for (int i = 1; i < count; i++) {
            TableRow row = originRow.deepClone();
            table.getRows().insert(originRowInt + i, row);
        }

        doc.saveToFile(filePath, FileFormat.Docx);
    }
}
